package com.hellobike.haxing.modules.order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hellobike.haxing.common.constant.CommonConstant;
import com.hellobike.haxing.common.system.query.QueryGenerator;
import com.hellobike.haxing.common.util.DateUtils;
import com.hellobike.haxing.common.vo.Result;
import com.hellobike.haxing.modules.order.constant.OrderConstants;
import com.hellobike.haxing.modules.order.entity.Order;
import com.hellobike.haxing.modules.order.entity.OrderAftersale;
import com.hellobike.haxing.modules.order.entity.OrderAftersaleDetail;
import com.hellobike.haxing.modules.order.mapper.*;
import com.hellobike.haxing.modules.order.send.OrderAfterSuccessCSend;
import com.hellobike.haxing.modules.order.service.IOrderAftersaleImgService;
import com.hellobike.haxing.modules.order.service.IOrderAftersaleService;
import com.hellobike.haxing.modules.order.service.IOrderCouponService;
import com.hellobike.haxing.modules.order.service.IOrderRefundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @Description: 售后订单
 * @Author: haxing-boot
 * @Date:   2020-02-08
 * @Version: V1.0
 */
@Service
public class OrderAftersaleServiceImpl extends ServiceImpl<OrderAftersaleMapper, OrderAftersale> implements IOrderAftersaleService {
    @Autowired
    private OrderAftersaleMapper orderAftersaleMapper;
    @Autowired
    private OrderAftersaleDetailMapper orderAftersaleDetailMapper;
    @Autowired
    private IOrderAftersaleImgService orderAftersaleImgService;
    @Autowired
    private IOrderCouponService orderCouponService;
    @Autowired
    private IOrderRefundService orderRefundService;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private OrderRefundMapper orderRefundMapper;
    @Autowired
    private OrderAfterSuccessCSend orderAfterSuccessCSend;
    @Override
    public IPage<OrderAftersale> queryPage(OrderAftersale orderAftersale, Page<OrderAftersale> page, Map<String, String[]> parameterMap) {
        QueryWrapper<OrderAftersale> queryWrapper = QueryGenerator.initQueryWrapper(orderAftersale, parameterMap);
        IPage<OrderAftersale> pageList = orderAftersaleMapper.queryPage(page,queryWrapper);
        return pageList;
    }

    @Override
    public List<OrderAftersale> queryList(Long orderId) {
        QueryWrapper<OrderAftersale> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(OrderAftersale::getOrderId,orderId).eq(OrderAftersale::getDeleted, CommonConstant.DEL_FLAG_0);
        return super.list(queryWrapper);
    }

    @Override
    public OrderAftersale getAftersaleById(Long id) {
        OrderAftersale orderAftersale = orderAftersaleMapper.getOrderById(id);
        //查询售后订单明细
        List<OrderAftersaleDetail> detailList = orderAftersaleDetailMapper.queryDetailList(id);
        orderAftersale.setAfterDetailList(detailList);
        List<String> imgLinkHref = orderAftersaleImgService.getLinkHrefList(id);
        orderAftersale.setImgLinkHref(imgLinkHref);
        return orderAftersale;
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> auditApprove(OrderAftersale orderAftersale) {
        OrderAftersale orderAfter=super.getById(orderAftersale.getId());
        if(!OrderConstants.ORDER_STATUS_18.equals(orderAfter.getAuditStatus())){
            return Result.error("已审核不允许重复审核");
        }
        orderAfter.setReturnAmount(orderAftersale.getReturnAmount());
        //判断退款金额是否大于订单金额(退款金额<=订单总金额-退款成功金额)
        Order order=orderMapper.selectById(orderAfter.getOrderId());
        if(OrderConstants.ORDER_STATUS_6.equals(order.getStatus())){
            return Result.error("售后已完成订单不允许审核通过，只允许审核拒绝！");
        }
        //已退款或者退款中的金额
       BigDecimal refundSuccess= orderRefundMapper.getSumRefundSuccess(orderAfter.getOrderId());
       //允许退款金额
        BigDecimal yrefund= order.getPayAmount().subtract(refundSuccess==null?BigDecimal.ZERO:refundSuccess);
        if(orderAfter.getReturnAmount().compareTo(yrefund)>0){
            return Result.ok("审核通过操作失败；退款金额大于可退款金额["+yrefund+"]元");
        }
        orderAfter.setRemark(orderAftersale.getRemark());
        orderAfter.setAuditStatus(OrderConstants.ORDER_STATUS_17);
        orderAfter.setAuditTime(DateUtils.getDate());
        orderAfter.setAfterStatus(OrderConstants.ORDER_STATUS_15);
        orderAfter.setAfterSubStatus(OrderConstants.ORDER_STATUS_20);
        orderAfter.setAuditUser(orderAftersale.getAuditUser());
        orderAfter.setReturnAmount(orderAftersale.getReturnAmount());
        orderAfter.setAuditAfterType(orderAftersale.getAuditAfterType());
        /**
         * 添加退款记录
         * 全部退货时-退优惠券
         */
        if(OrderConstants.ORDER_AFTERSALE_TYPE_1.equals(orderAfter.getType())){
            orderCouponService.returnCoupon(orderAfter.getOrderId());
        }
        orderRefundService.saveRefundAmount(orderAfter);
        orderMapper.updateOrderStatus(orderAfter.getOrderId(),orderAfter.getAfterStatus());
        super.updateById(orderAfter);
        orderAfterSuccessCSend.sendOrderAfterSuccess(orderAfter.getId());
        return Result.ok(orderAfter);
    }
    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result<?> auditReject(OrderAftersale orderAftersale) {
        OrderAftersale orderAfter=super.getById(orderAftersale.getId());
        if(!OrderConstants.ORDER_STATUS_18.equals(orderAfter.getAuditStatus())){
            return Result.error("已审核不允许重复审核");
        }
        orderAfter.setRemark(orderAftersale.getRemark());
        orderAfter.setAuditStatus(OrderConstants.ORDER_STATUS_16);
        orderAfter.setAuditTime(DateUtils.getDate());
        orderAfter.setAfterStatus(OrderConstants.ORDER_STATUS_14);
        orderAfter.setAfterSubStatus(OrderConstants.ORDER_STATUS_16);
        orderAfter.setAuditUser(orderAftersale.getAuditUser());
        orderAfter.setAuditAfterType(orderAftersale.getAuditAfterType());
        super.updateById(orderAfter);
        orderMapper.updateOrderStatus(orderAfter.getOrderId(),orderAfter.getAfterStatus());
        return Result.ok(orderAfter);
    }
}
